/**
 * Class: CalendarUI
 * 
 * @author Ryne Okimoto
 * 
 * Description: GUI for the .ics file creator application
 */

package nb314calendargui;

import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat;

public class CalendarUI extends javax.swing.JFrame 
{
    /* Class data */
    private iCalEvent calEvent;
    
    /* Strings to be written to .ics file */
    private String eventName;
    private String resources;
    private String location;
    private String desc;
    private String priority;
    private String privacy;
    private String timeStart, timeEnd;
    private String dateStart, dateEnd;
    
    /* Variables for time and date components */
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss");
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    private GregorianCalendar gCal = new GregorianCalendar();
    private GregorianCalendar today = new GregorianCalendar(
            gCal.get(Calendar.YEAR), gCal.get(Calendar.MONTH),
            gCal.get(Calendar.DAY_OF_MONTH));
    
    private GregorianCalendar cal = new GregorianCalendar();
    private Date d1;
    private SpinnerDateModel StartDateModel;
    private SpinnerDateModel EndDateModel;

    /**
     * Creates new form CalendarUI
     * 
     * Default constructor
     */
    public CalendarUI() {
        
        /* Initialize all strings to null */
        eventName    = null;
        resources    = null;
        location     = null;
        desc         = null;
        priority     = null;
        privacy      = null;
        timeStart    = null;
        timeEnd      = null;
        dateStart    = null;
        dateEnd      = null;
        calEvent     = null;
        
        /* Initialize the start and end times to 12:00 AM */
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        d1 = cal.getTime();
        StartDateModel = 
            new SpinnerDateModel(d1, null, null, Calendar.HOUR_OF_DAY);
        EndDateModel =
            new SpinnerDateModel(d1, null, null, Calendar.HOUR_OF_DAY);
        
        /* Initialize the rest of the GUI components */
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        locationPanel = new javax.swing.JPanel();
        locationLabel = new javax.swing.JLabel();
        locationField = new javax.swing.JTextField();
        namePanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        resourcePanel = new javax.swing.JPanel();
        resourceLabel = new javax.swing.JLabel();
        resourceField = new javax.swing.JTextField();
        descPanel = new javax.swing.JPanel();
        descLabel = new javax.swing.JLabel();
        descTextArea = new javax.swing.JScrollPane();
        descArea = new javax.swing.JTextArea();
        priorityPanel = new javax.swing.JPanel();
        priorityLabel = new javax.swing.JLabel();
        priorityMenu = new javax.swing.JComboBox();
        privacyPanel = new javax.swing.JPanel();
        privacyLabel = new javax.swing.JLabel();
        privacyMenu = new javax.swing.JComboBox();
        createButton = new javax.swing.JButton();
        datePanel = new javax.swing.JPanel();
        startdateLabel = new javax.swing.JLabel();
        enddateLabel = new javax.swing.JLabel();
        startDate = new datechooser.beans.DateChooserCombo();
        endDate = new datechooser.beans.DateChooserCombo();
        timePanel = new javax.swing.JPanel();
        starttimeLabel = new javax.swing.JLabel();
        endtimeLabel = new javax.swing.JLabel();
        starttime = new javax.swing.JSpinner(StartDateModel);
        endtime = new javax.swing.JSpinner(EndDateModel);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Eventer");
        setResizable(false);

        locationLabel.setText("Location");

        locationField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                locationFieldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout locationPanelLayout = new org.jdesktop.layout.GroupLayout(locationPanel);
        locationPanel.setLayout(locationPanelLayout);
        locationPanelLayout.setHorizontalGroup(
            locationPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(locationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(locationLabel)
                .add(37, 37, 37)
                .add(locationField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        locationPanelLayout.setVerticalGroup(
            locationPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(locationPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(locationPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(locationLabel)
                    .add(locationField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        nameLabel.setText("Event Name");

        nameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameFieldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout namePanelLayout = new org.jdesktop.layout.GroupLayout(namePanel);
        namePanel.setLayout(namePanelLayout);
        namePanelLayout.setHorizontalGroup(
            namePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(namePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(nameLabel)
                .add(18, 18, 18)
                .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        namePanelLayout.setVerticalGroup(
            namePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(namePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(namePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        resourceLabel.setText("Resources");

        resourceField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resourceFieldActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout resourcePanelLayout = new org.jdesktop.layout.GroupLayout(resourcePanel);
        resourcePanel.setLayout(resourcePanelLayout);
        resourcePanelLayout.setHorizontalGroup(
            resourcePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(resourcePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(resourceLabel)
                .add(26, 26, 26)
                .add(resourceField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 311, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        resourcePanelLayout.setVerticalGroup(
            resourcePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(resourcePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(resourcePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(resourceLabel)
                    .add(resourceField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        descLabel.setText("Description");

        descArea.setColumns(20);
        descArea.setRows(5);
        descTextArea.setViewportView(descArea);

        org.jdesktop.layout.GroupLayout descPanelLayout = new org.jdesktop.layout.GroupLayout(descPanel);
        descPanel.setLayout(descPanelLayout);
        descPanelLayout.setHorizontalGroup(
            descPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(descPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(descLabel)
                .add(18, 18, 18)
                .add(descTextArea, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 306, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        descPanelLayout.setVerticalGroup(
            descPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(descPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(descPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(descTextArea, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                    .add(descPanelLayout.createSequentialGroup()
                        .add(descLabel)
                        .add(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        priorityLabel.setText("Priority");

        priorityMenu.setMaximumRowCount(4);
        priorityMenu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None", "Low", "Medium", "High" }));
        priorityMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priorityMenuActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout priorityPanelLayout = new org.jdesktop.layout.GroupLayout(priorityPanel);
        priorityPanel.setLayout(priorityPanelLayout);
        priorityPanelLayout.setHorizontalGroup(
            priorityPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(priorityPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(priorityLabel)
                .add(44, 44, 44)
                .add(priorityMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        priorityPanelLayout.setVerticalGroup(
            priorityPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(priorityPanelLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(priorityPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(priorityLabel)
                    .add(priorityMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        privacyLabel.setText("Privacy");

        privacyMenu.setMaximumRowCount(3);
        privacyMenu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Public", "Private", "Confidential" }));
        privacyMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                privacyMenuActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout privacyPanelLayout = new org.jdesktop.layout.GroupLayout(privacyPanel);
        privacyPanel.setLayout(privacyPanelLayout);
        privacyPanelLayout.setHorizontalGroup(
            privacyPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(privacyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(privacyLabel)
                .add(45, 45, 45)
                .add(privacyMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        privacyPanelLayout.setVerticalGroup(
            privacyPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(privacyPanelLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(privacyPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(privacyLabel)
                    .add(privacyMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );

        createButton.setText("Create Event");
        createButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                createButtonMouseClicked(evt);
            }
        });
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        startdateLabel.setText("Start Date");

        enddateLabel.setText("End Date");

        startDate.setNothingAllowed(false);
        startDate.setBehavior(datechooser.model.multiple.MultyModelBehavior.SELECT_SINGLE);
        startDate.setCurrent(today);
        startDate.setToolTipText("Click on icon to select date");

        endDate.setNothingAllowed(false);
        endDate.setBehavior(datechooser.model.multiple.MultyModelBehavior.SELECT_SINGLE);
        endDate.setCurrent(today);
        endDate.setToolTipText("Click icon to select date");

        org.jdesktop.layout.GroupLayout datePanelLayout = new org.jdesktop.layout.GroupLayout(datePanel);
        datePanel.setLayout(datePanelLayout);
        datePanelLayout.setHorizontalGroup(
            datePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(datePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(datePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(datePanelLayout.createSequentialGroup()
                        .add(enddateLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(endDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(datePanelLayout.createSequentialGroup()
                        .add(startdateLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 30, Short.MAX_VALUE)
                        .add(startDate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
        );
        datePanelLayout.setVerticalGroup(
            datePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(datePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(datePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(startDate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(startdateLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(datePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(endDate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(enddateLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(0, 52, Short.MAX_VALUE))
        );

        starttimeLabel.setText("Start Time");

        endtimeLabel.setText("End Time");

        JSpinner.DateEditor startDateEdit = new JSpinner.DateEditor(starttime, "h:mm a");
        starttime.setEditor(startDateEdit);
        starttime.setToolTipText("Use arrows to select time");

        JSpinner.DateEditor endDateEdit = new JSpinner.DateEditor(endtime, "h:mm a");
        endtime.setEditor(endDateEdit);
        endtime.setToolTipText("Use arrows to select time");

        org.jdesktop.layout.GroupLayout timePanelLayout = new org.jdesktop.layout.GroupLayout(timePanel);
        timePanel.setLayout(timePanelLayout);
        timePanelLayout.setHorizontalGroup(
            timePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(timePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(timePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(starttimeLabel)
                    .add(endtimeLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(timePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(starttime)
                    .add(endtime))
                .addContainerGap())
        );
        timePanelLayout.setVerticalGroup(
            timePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(timePanelLayout.createSequentialGroup()
                .add(7, 7, 7)
                .add(timePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(starttime)
                    .add(starttimeLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(timePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(endtime)
                    .add(endtimeLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(namePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(locationPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(resourcePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(privacyPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(createButton)
                        .add(descPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(priorityPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(datePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(timePanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(namePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(locationPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(resourcePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(datePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(priorityPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(28, 28, 28)
                        .add(timePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(privacyPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(descPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(createButton)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * Method name: resetFields
     * 
     * @param none
     * @return none
     * 
     * Description: Resets all fields of the GUI
     */
    private void resetFields()
    {
        /* Reset text fields, text area, and drop down menus */
        nameField.setText("");
        resourceField.setText("");
        locationField.setText("");
        descArea.setText("");
        priorityMenu.setSelectedIndex(0);
        privacyMenu.setSelectedIndex(0);
        
        /* Reset start and end date to today */
        startDate.setSelectedDate(today);
        endDate.setSelectedDate(today);
        
        /* Reset start and end time */
        starttime.getModel().setValue(d1);
        endtime.getModel().setValue(d1);
    }

    /**
     * Method name: validateInput
     * 
     * @param none
     * @return boolean True if the time and date input is valid
     *                 False otherwise
     * 
     * Description: Checks for valid dates and times upon user submission
     */
    private boolean validateInput()
    {
        boolean pass = true;
        
        /* Get the values in the start date and end date fields*/
        Calendar calStart      = startDate.getSelectedDate();
        Calendar calEnd        = endDate.getSelectedDate();
        Calendar timestartCal  = new GregorianCalendar();
        Calendar timeendCal    = new GregorianCalendar();
        
        timestartCal.setTime((Date) starttime.getValue());
        timeendCal.setTime((Date) endtime.getValue());
        
        Date     dStart        = startDate.getSelectedDate().getTime();
        Date     dEnd          = endDate.getSelectedDate().getTime();
        
        long     numStartDate  = calStart.getTimeInMillis();
        long     numEndDate    = calEnd.getTimeInMillis();
        
        /* Get the start time and end time */
        int      hourStart     = timestartCal.get(Calendar.HOUR_OF_DAY);
        int      hourEnd       = timeendCal.get(Calendar.HOUR_OF_DAY);
        
        int      minStart      = timestartCal.get(Calendar.MINUTE);
        int      minEnd        = timeendCal.get(Calendar.MINUTE);
        
        if(numStartDate <= numEndDate)
        {
            // Start date is the same as end date or before end date
            
            if(calStart.get(Calendar.DAY_OF_YEAR) <= calEnd.get(Calendar.DAY_OF_YEAR))
            {
                // Start date is the same as the end date
                
                if(hourStart <= hourEnd)
                {
                    // Start time must be before end time
                    
                    if(hourStart == hourEnd)
                    {
                        // Start hour is the same as end hour
                        
                        if(minStart <= minEnd)
                        {
                            // Start minute must be before end minute
                        }
                        else
                        {
                            pass = false;
                            JOptionPane.showMessageDialog(this, "Start time"
                                    + " (minute) must be before or equal to "
                                    + " end time (minute)");
                        }
                    }
                }
                else
                {
                    pass = false;
                    JOptionPane.showMessageDialog(this, "Start time (hour)"
                            + " must be before or equal to end time (hour)");
                }
            }
        }
        else
        {
            pass = false;
            JOptionPane.showMessageDialog(this, "Start date must be before or "
                    + "equal to end date");
        }
        
        return pass;
    }
    private void locationFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_locationFieldActionPerformed

    }//GEN-LAST:event_locationFieldActionPerformed

    private void nameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nameFieldActionPerformed

    private void resourceFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resourceFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_resourceFieldActionPerformed

    private void priorityMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priorityMenuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_priorityMenuActionPerformed

    private void privacyMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_privacyMenuActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_privacyMenuActionPerformed

    /**
     * Method name: createButtonActionPerformed
     * 
     * @param evt The event
     * @return none
     * 
     * Description: Validates date/time input and creates a .ics file
     */
    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        
        /* Creation of .ics file begins only when date/times are valid */
        if(validateInput())
        {
            int priorityIndex = priorityMenu.getSelectedIndex();
            
            /* Get input from text fields */
            eventName    = nameField.getText();
            resources    = resourceField.getText();
            location     = locationField.getText();
            desc         = descArea.getText();
            
            /* Convert the priority to the format for the .ics file */
            if(priorityIndex == 0)
            {
                priority = "0";
            }
            else if(priorityIndex == 1)
            {
                priority = "9";
            }
            else if(priorityIndex == 2)
            {
                priority = "5";
            }
            else
            {
                priority = "1";
            }
            
            /* Get input from the privacy dropdown and the date/time fields */
            privacy      = ((String) privacyMenu.getSelectedItem()).toUpperCase();
            dateStart    = dateFormat.format(startDate.getCurrent().getTime());
            timeStart    = timeFormat.format((Date) starttime.getValue());
            dateEnd      = dateFormat.format(endDate.getCurrent().getTime());
            timeEnd      = timeFormat.format((Date) endtime.getValue());

            /* For debugging. Popup to show passed parameters
            JOptionPane.showMessageDialog(this, "Parameters submitted\n\n" +
                "Event Title: " + eventName + "\n\n" +
                "Location: "    + location  + "\n\n" +
                "Resources: "   + resources + "\n\n" +
                "Start Date: "  + dateStart + "\n\n" +
                "Start Time: "  + timeStart + "\n\n" +
                "End Date: "    + dateEnd   + "\n\n" +
                "End Time: "    + timeEnd   + "\n\n" +
                "Priority: "    + priority  + "\n\n" +
                "Privacy: "     + privacy   + "\n\n" +
                "Description: " + desc);
            */
            
            /* Create the .ics file */
            calEvent = new iCalEvent(eventName, dateStart, dateEnd, timeStart,
                    timeEnd, location, resources, privacy, priority, desc);
            if(calEvent.createFile())
            {
                JOptionPane.showMessageDialog(this, "Event created successfully");
            }
            resetFields();
        }
    }//GEN-LAST:event_createButtonActionPerformed

    private void createButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_createButtonMouseClicked
 
    }//GEN-LAST:event_createButtonMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createButton;
    private javax.swing.JPanel datePanel;
    private javax.swing.JTextArea descArea;
    private javax.swing.JLabel descLabel;
    private javax.swing.JPanel descPanel;
    private javax.swing.JScrollPane descTextArea;
    private datechooser.beans.DateChooserCombo endDate;
    private javax.swing.JLabel enddateLabel;
    private javax.swing.JSpinner endtime;
    private javax.swing.JLabel endtimeLabel;
    private javax.swing.JTextField locationField;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JPanel locationPanel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JPanel namePanel;
    private javax.swing.JLabel priorityLabel;
    private javax.swing.JComboBox priorityMenu;
    private javax.swing.JPanel priorityPanel;
    private javax.swing.JLabel privacyLabel;
    private javax.swing.JComboBox privacyMenu;
    private javax.swing.JPanel privacyPanel;
    private javax.swing.JTextField resourceField;
    private javax.swing.JLabel resourceLabel;
    private javax.swing.JPanel resourcePanel;
    private datechooser.beans.DateChooserCombo startDate;
    private javax.swing.JLabel startdateLabel;
    private javax.swing.JSpinner starttime;
    private javax.swing.JLabel starttimeLabel;
    private javax.swing.JPanel timePanel;
    // End of variables declaration//GEN-END:variables
}
